package com.pc.controller;


import com.pc.entity.Orderlist;
import com.pc.service.OrderListService;
import com.pc.utils.OrderFore;
import com.pc.utils.Result;
import com.pc.utils.aop.OperLog;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("order")
public class OrderListController {
    @Resource
    private OrderListService orderListService;

    @PostMapping("/addOrder")
    @OperLog(operModul = "订单模块",operType = "增加",operDesc = "增加订单")
    public Result addOrder(@RequestBody OrderFore[] obj){

        Date date = new Date(); SimpleDateFormat dateFormat= new
                SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");

        Orderlist order1 = new Orderlist();
        order1.setFoodname(obj[0].getName());
        order1.setFoodmoney(obj[0].getPrice());
        order1.setCreatedate(dateFormat.format(date));


        for (int i = 0; i < obj.length; i++) {
            Orderlist orderlist = new Orderlist();
            orderlist.setFoodname(obj[i].getName());
            orderlist.setFoodmoney(obj[i].getPrice());
            orderlist.setCreatedate(dateFormat.format(date));

            boolean b = orderListService.addOrder(orderlist);
            if(!b)
                return Result.fail("订单记录添加失败");
        }
        return Result.success("订单记录添加成功");
    }

}
